package com.zhongzhi.javafx.controller;

import java.net.URL;
import java.util.ResourceBundle;

import com.zhongzhi.bean.Configuration;
import com.zhongzhi.common.Common;
import com.zhongzhi.util.Bo;
import com.zhongzhi.util.ClientUtil;
import com.zhongzhi.util.ConfigUtil;
import com.zhongzhi.util.JsonResult;
import com.zhongzhi.util.Verify;

import de.felixroske.jfxsupport.FXMLController;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.TextField;
import javafx.stage.Stage;
import lombok.extern.log4j.Log4j;
import javafx.scene.control.Label;
@Log4j
public class ConnectionFailController implements Initializable{
	@FXML
	private TextField serial;			//站号
	@FXML
	private TextField ServerId;			//服务器ip
	@FXML
	private TextField ServerProt;		//服务器端口
	@FXML
	private Button exit;				//退出系统
	@FXML
	private Button ser;					//保存参数
	@FXML
	private Button text;				//测试连接
	@FXML
	private Label serveridtext;			//ip地址
	@FXML
	private Label serverprottext;		//端口
    @FXML
    private Label hint;					//失败提示
    private String port;
    
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		button();
		
	}
	private void button() {
		Configuration configuration = Common.getConfiguration();
		serial.setText(configuration.getProperty());
		ServerId.setText(configuration.getServerId());
		ServerProt.setText(configuration.getServerProt());
		
		
		exit.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				
				log.info("关闭程序");
				Stage window = (Stage)hint.getScene().getWindow();
				window.close();
				window = (Stage)Common.getSp().getScene().getWindow();
				window.close();
				System.exit(0);
			}
		});
		
		ser.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				if (Verify.ipIsNotIp(ServerId.getText())) {
					serveridtext.setText("IP地址格式不正确");
				}else {
					serveridtext.setText("");
					if (Verify.NumisNO(ServerProt.getText())) {
						serverprottext.setText("端口必须为纯数字");
					}else {
						serverprottext.setText("");
						ConfigUtil configUtil = new ConfigUtil();
//						configUtil.setConfiguration(serial.getText(), ServerId.getText(), ServerProt.getText());
						configUtil.setConfiguration(
								serial.getText(), 
								ServerId.getText(),
								ServerProt.getText(),
								configuration.getScope1().toString(),
								configuration.getScope2().toString(),
								configuration.getScope3().toString(),
								configuration.getScope4().toString(),
								configuration.getScope5().toString(),
								configuration.getScope6().toString(),
								configuration.getMod1().toString(),
								configuration.getMod2().toString(),
								configuration.getMod3().toString(),
								configuration.getMod4().toString(),
								configuration.getMod5().toString(),
								configuration.getMod6().toString(),
								configuration.getMod7().toString());
					}
				}
			}
		});
		//测试
		text.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				try {
					log.info("测试连接远端服务器");
					hint.setText("");
					String string  = "http://"+ServerId.getText().trim()+":"+ServerProt.getText().trim()+"/doosanRadar/ds/teston";
					String gethttp = ClientUtil.Gethttp(string);
					JsonResult jsonobj = ClientUtil.Jsonobj(gethttp, JsonResult.class);
					if (jsonobj.getCode()==200) {
						log.info("主画面:测试连接远端服务器成功");
						configuration.setBoolean1(true);
						
						ConfigUtil configUtil = new ConfigUtil();
//						configUtil.setConfiguration(serial.getText(), ServerId.getText(), ServerProt.getText());
						configUtil.setConfiguration(
								serial.getText(), 
								ServerId.getText(),
								ServerProt.getText(),
								configuration.getScope1().toString(),
								configuration.getScope2().toString(),
								configuration.getScope3().toString(),
								configuration.getScope4().toString(),
								configuration.getScope5().toString(),
								configuration.getScope6().toString(),
								configuration.getMod1().toString(),
								configuration.getMod2().toString(),
								configuration.getMod3().toString(),
								configuration.getMod4().toString(),
								configuration.getMod5().toString(),
								configuration.getMod6().toString(),
								configuration.getMod7().toString());
						
						Stage window = (Stage)hint.getScene().getWindow();
						window.close();
					}else {
						hint.setText("连接远程服务器失败");
						log.info("主画面:测试连接远端服务器失败");
					}
					
				} catch (Exception e) {
					log.error("主画面:测试连接远端服务器失败:"+e);
				}
				
			}
		});
		
		
		
	}
}
